﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace DMS.BEs
{
	public class Invoice : Persistent
	{
		private readonly IList<InvoiceLine> lines;

		public Invoice()
		{
			lines = new List<InvoiceLine>();
		}

		public virtual Customer Customer { get; set; }
		public virtual DateTime InvoiceDate { get; set; }

		public virtual string BillingAddress { get; set; }
		public virtual string BillingCity { get; set; }
		public virtual string BillingState { get; set; }
		public virtual string BillingCountry { get; set; }
		public virtual string BillingPostalCode { get; set; }

		public virtual ICollection<InvoiceLine> Lines
		{
			get { return lines; }
		}

		public virtual decimal GetTotal()
		{
			return Lines.Sum(l => l.Quantity*l.UnitPrice);
		}


		public virtual void AddLine(InvoiceLine line)
		{
			line.Invoice = this;
			Lines.Add(line);
		}

		public virtual void AddLines(IEnumerable<InvoiceLine> newLines)
		{
			foreach (var line in newLines)
				AddLine(line);
		}

		public virtual void RemoveLine(int index)
		{
			lines.RemoveAt(index);
		}

		public virtual InvoiceLine GetLine(int index)
		{
			return lines[index];
		}
	}
}